package com.alibaba.datax.core.transport.transformer;

import com.alibaba.datax.common.element.Column;
import com.alibaba.datax.common.element.Record;
import com.alibaba.datax.common.element.StringColumn;
import com.alibaba.datax.common.exception.DataXException;
import com.alibaba.datax.transformer.Transformer;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

/**
 * str cut.
 */
public class CutstrTransformer extends Transformer {
    public CutstrTransformer() {
        setTransformerName("dx_cutstr");
    }

    @Override
    public Record evaluate(Record record, Object... paras) {

        int columnIndex;
        int order;
        String startStr;
        int startIndex;
        int length;

        try {
            if (paras.length != 5) {
                throw new RuntimeException("dx_substr paras must be 5");
            }

            columnIndex = (Integer) paras[0];
            order = Integer.valueOf((String) paras[1]);
            startStr = (String) paras[2];
            startIndex = Integer.valueOf((String) paras[3]);
            length = Integer.valueOf((String) paras[4]);

        } catch (Exception e) {
            throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER, "paras:" + Arrays.asList(paras).toString() + " => " + e.getMessage());
        }

        Column column = record.getColumn(columnIndex);

        try {
            String oriValue = column.asString();
            //如果字段为空，跳过subStr处理
            if(oriValue == null){
                return record;
            }
            String newValue;
            if (order == 0){
                if (StringUtils.isNotBlank(startStr)){
                    startIndex = oriValue.indexOf(startStr) + 1 ;
                }
                if (startIndex > oriValue.length() -1) {
                    throw new RuntimeException(String.format("dx_substr startIndex(%s) out of range(%s)", startIndex, oriValue.length()));
                }
                if (startIndex >= 0){
                    if (startIndex + length >= oriValue.length()) {
                        newValue = oriValue.substring(startIndex, oriValue.length());
                    } else {
                        newValue = oriValue.substring(startIndex, startIndex + length);
                    }
                }else {
                    newValue = oriValue;
                }
            }else {
                if (StringUtils.isNotBlank(startStr)){
                    startIndex = oriValue.indexOf(startStr) - 1 ;
                }
                if (startIndex > -1){
                    // 9999则默认为字符串长度
                    if (startIndex == 9999){
                        startIndex = oriValue.length() -1;
                    }
                    int leftIndex = startIndex - length + 1 > -1 ? startIndex - length + 1 : 0;
                    newValue = oriValue.substring(leftIndex, startIndex + 1);
                }else {
                    newValue = oriValue;
                }
            }


            record.setColumn(columnIndex, new StringColumn(newValue));

        } catch (Exception e) {
            throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_RUN_EXCEPTION, e.getMessage(),e);
        }
        return record;
    }
}
